package com.longma.server.controller;

import com.longma.server.pojo.UserResponseEntity;
import com.longma.server.service.IFileService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/api/file")
public class FileController {

    @Autowired
    private IFileService iFileService;

    /**
     * @Author： zzc
     * @Date：2021/5/15
     * @Description：上传图片
     * @param file
     * @return: ResponseEntity<String>
     */
    @ApiOperation(value = "上传文件")
    @PostMapping("/upload")
    public UserResponseEntity<String> upload(@ApiParam("上传文件") @NotNull(message = "文件不能为空") @RequestPart("file") MultipartFile file) {
        String fileName = iFileService.uploadFile(file);
        return UserResponseEntity.success(fileName);
    }

    /**
     * @Author： zzc
     * @Date：2021/5/15
     * @Description：下载图片
     * @param fileName
     * @param response
     * @return: void
     */
    @ApiOperation(value = "下载文件")
    @GetMapping("/download/{filename}")
    public void download(@ApiParam("下载文件") @NotNull(message = "文件名不能为空") @PathVariable("filename") String fileName, HttpServletResponse response) {
        iFileService.downloadFile(response,fileName);
    }
}
